<template>
  <div class="editTopic">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="主标题">
        <el-input v-model="TopicInfo.title"></el-input>
      </el-form-item>
      <el-form-item label="副标题">
        <el-input v-model="TopicInfo.subtitle"></el-input>
      </el-form-item>
      <el-form-item label="品牌图片">
        <el-upload
          class="avatar-uploader"
          action="http://kumanxuan1.f3322.net:8360/admin/upload/goodNewPic"
          name="good_pic"
          :headers="token"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="最低价">
        <el-input v-model="TopicInfo.price_info"></el-input>
      </el-form-item>
      <el-form-item label="显示">
        <el-switch v-model="form.delivery"></el-switch>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="TopicInfo.sort_order"></el-input>
      </el-form-item>
      <el-form-item label="品牌描述">
        <Editor :editorsdata="editorsdata" @childemit="parentEvent"> </Editor>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onServe(TopicInfo)">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Editor from "../../components/editor.vue";
import { getTopicInfo, addTopic } from "../../request/httpApi";

export default {
  components: {
    Editor,
  },
  data() {
    return {
      str: "st",
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      imageUrl: "",
      TopicInfo: {},
      token: {},
      imgurlp: null,
      editorsdata: null,
    };
  },
  methods: {
    parentEvent(data) {
      this.editorsdata = data;
      console.log(this.editorsdata);
    },
    onServe(val) {
      // console.log(11);
      console.log(val);
      addTopic({
        avatar: val.avatar,
        content:this.editorsdata,
        id: val.id,
        is_show: val.is_show,
        item_pic_url: val.item_pic_url,
        price_info: val.price_info,
        read_count: val.read_count,
        scene_pic_url: this.imageUrl,
        sort_order: val.sort_order,
        subtitle: val.subtitle,
        title: val.title,
        topic_category_id: val.topic_category_id,
        topic_tag_id: val.topic_tag_id,
        topic_template_id: val.topic_template_id,
      }).then((res) => {
        console.log(res);
      });
      this.$router.push('/topic')
    },
    handleAvatarSuccess(file) {
      this.imageUrl = file.data.fileUrl;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.token = {
      "X-Nideshop-Token": localStorage.getItem("x-auth-token"),
    };

    getTopicInfo({
      id: this.$route.params.tid,
    }).then((res) => {
      // console.log(res);
      if (res.errno == 0) {
        this.TopicInfo = res.data;
        this.editorsdata = res.data.content;
        // this.editorsdata=this.TopicInfo.map(item=>{
        //   return item.content
        // })
        console.log(this.TopicInfo.content);
        // console.log(this.editorsdata);
        this.imageUrl = res.data.scene_pic_url;
      }
    });
  },
};
</script>
 
<style lang = "less" scoped>
.editTopic {
  background-color: #fff;
  padding-top: 10px;
  padding-left: 20px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader {
  border: 1px solid #dcdfe6;
  width: 170px;
}
/deep/ .el-input__inner {
  width: 200px;
}
</style>